"""
    @Author  ：思念 
    @File    ：5.使用aiohttp爬取豆瓣.py
    @Date    ：2024/12/28 19:49 
"""

import aiohttp
import asyncio
from bs4 import BeautifulSoup

url = 'https://movie.douban.com/top250?start={}&filter='
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
}


def callback(future):
    for temp in future.result():
        print(temp.get_text())


async def get_movie_info(page_num):
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url.format(page_num), headers=headers) as response:
            soup = BeautifulSoup(await response.text(), 'lxml')
            div_list = soup.find_all('div', class_='hd')
            # for movie in soup.find_all('div', class_='hd'):
            #     print(movie.find('span', class_='title').get_text())
            return div_list


async def main():
    tasks = [asyncio.create_task(get_movie_info(page)) for page in range(5)]
    for temp in tasks:
        temp.add_done_callback(callback)
    await asyncio.wait(tasks)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
